<?php

header("Content-type:application/json; charset=UTF-8");          
header("Cache-Control: no-store, no-cache, must-revalidate");         
header("Cache-Control: post-check=0, pre-check=0", false);  

require('../service/category_service.php');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$wsname = $_REQUEST['wsname'];
$tokeyKey = $_REQUEST['token'];

class category_ws
{

    public function getAllCategory()
    {
        $category_service = new category_service();
        $lists = $category_service->findAll();
        return apputil::getJsonDataResultUtil($lists, 'categories');
    }

    public function getCategoryById($id)
    {
        $category_service = new category_service();
        $lists = $category_service->findCategoryById($id);
        return apputil::getJsonDataResultUtil($lists, 'categories');
    }
    
    public function createCategory($categoryObject) {
        $category_service = new category_service();
        $flag = $category_service->create($categoryObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }
    
    public function updateCategory($categoryObject) {
        $category_service = new category_service();
        $flag = $category_service->update($categoryObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }
    
    public function deleteCategory($categoryObject) {
        $category_service = new category_service();
        $flag = $category_service->delete($categoryObject);
        return apputil::getJsonDataResultUtil($flag, 'success');
    }

    public function isValidateToken($tokeyKey)
    {
        $category_service = new category_service();
        return $category_service->isValidateTokenKey($tokeyKey);
    }

}

//init class
$category_ws = new category_ws();

if ($category_ws->isValidateToken($tokeyKey)) {
    if ($wsname == 'getallcategories') {
        print_r($category_ws->getAllCategory());
    } else if ($wsname == 'getcategorybyid') {
        print_r($category_ws->getCategoryById($_REQUEST['id']));
    } else if ($wsname == 'createcategory' || $wsname == 'updatecategory') {
        $category = new Category();
        
        $category->setName(isset($_REQUEST['name']) ? $_REQUEST['name'] : null);
        $category->setTitle(isset($_REQUEST['title']) ? $_REQUEST['title'] : null);
        $category->setDescription(isset($_REQUEST['description']) ? $_REQUEST['description'] : null);

        if($wsname == 'createcategory') {
            print_r($category_ws->createCategory($category));
        } else if($wsname == 'updatecategory') {
            $category->setId(isset($_REQUEST['id']) ? $_REQUEST['id'] : null);
            print_r($category_ws->updateCategory($category));
        }
        
    } else if($wsname == 'deletecategory') {
        $category = new Category();
        $category->setId(isset($_REQUEST['id']) ? $_REQUEST['id'] : null);
        print_r($category_ws->deleteCategory($category));
    }else {
        echo 'web service error !!';
    }
} else {
    echo 'Your token is incorrect !!';
}
?>